<?php

namespace app\admin\model;

use think\Model;
use think\Config;

class Gonggao extends Model
{

    

    

    // 表名
    protected $name = 'gonggao';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];
    
        public function detail($id)
    {
        $dataout = self::get($id);
        $contents = $dataout['contents'];
        // return $contents;
        $string_pos = $this->getCharpos($contents,'/uploads');
        if(!empty($string_pos)){
            $new_contents = '';
       
            foreach ($string_pos as $key => $value) {
               $resultArra = $this->get_msg($contents,'src="'," alt=",$key+1,0);
            }
            
            foreach ($string_pos as $key => $value) {
                $new_contents .= '<img src="'.$resultArra[$key] . '/>';

            }
         
         $dataout['contents'] = $new_contents;
         return $dataout;
        }

    }

    /*
    *取字符串中间文本
    * @param    string  $str    需要处理的字符串
    * @param    string  $start_str 目标字符串‘前’的字符
    * @param    string  $stop_str  目标字符串‘后’的字符
    * @param    int     $count     需要查找的次数
    * @param    int     $n         从第n个位置开始查找
    * @return   array   $data   返回查找满足条件要求的数组字符串
    */
    public function get_msg($str,$start_str,$stop_str,$count,$n){
      $start=$n;  //从第n个位置开始查找
      $data=array();
      for($i=0;$i<$count;$i++){
             $start=strpos($str,$start_str,$start);
             $stop=strpos($str,$stop_str,$start);
             $start=strlen($start_str)+$start;
             $data[$i]= Config::get('site.cdnurl') . substr($str,$start,$stop-$start);
             $start=$stop;
        }
        return $data;
      }

    public function getCharpos($str,$char){

        $j = 0;
        $arr = [];
        $count = substr_count($str, $char);
        for ($i=0; $i < $count; $i++) { 
            $j = strpos($str,$char,$j);
            $arr[] = $j;
            $j = $j + 1;
        }
        return $arr;

    }

    







}
